home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Programming / Programming Languages / XLISP 2.0 / XLISP Tools / Utility (UL) / IO.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1988-04-07  |  8.0 KB  |  239 lines  |  [TEXT/ttxt]

  1. ;; Larry Mulcahy 1988
  2. ;; io
  3.  
  4. (provide 'io)
  5. (require 'format-long "long")
  6. (require 'msg)
  7. (require 'personality "personal")
  8. (require 'string)
  9. (require 'type)
  10.  
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ; display
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15.  
  16. (defun display (string &key echo-stream)
  17.   (princ string)
  18.   (and echo-stream (princ string echo-stream)))
  19.  
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ; read-non-empty-line
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23.  
  24. (defun read-non-empty-line ()
  25.   (prog (result)
  26.    loop (setq result (read-line))
  27.         (if (= (length result) 0)
  28.             (go loop))
  29.         (return result)))
  30.  
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32. ; ask
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34.  
  35. (defun ask (&key (type t) echo-stream default prompt quit-tag)
  36.   ; typep from the type module
  37.   (loop 
  38.    (let* ((result (prompt-with-default
  39.                    :default default
  40.                    :type type
  41.                    :prompt prompt
  42.                    :quit-tag quit-tag
  43.                    :echo-stream echo-stream))
  44.           (done (typep result type)))
  45.      (if done
  46.        (return result)                ; This is the exit from the loop
  47.        (insist-upon-correct-type type
  48.                                  :echo-stream echo-stream
  49.                                  :quittable? quit-tag)))))
  50.  
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ; get-char
  53. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  54.  
  55. (defun get-char () (int-char (get-key)))
  56.  
  57. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58. ; y-or-n-p
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60.  
  61. (defun y-or-n-p (&key prompt)
  62.   (if prompt (princ prompt))
  63.   (let ((result (get-one-of-these-characters '(#\y #\Y #\n #\N))))
  64.     (if (member result '(#\y #\Y))
  65.         (progn (princ "Yes") (terpri) t)
  66.         (progn (princ "No") (terpri) nil))))
  67.  
  68. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  69. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  70.  
  71. (defun display-array (arr)
  72.   (for i 0 (1- (length arr)) (msg "[" i "]: " (aref arr i) t)))
  73.  
  74. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  75. ; format-prompt
  76. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77.  
  78. (defun format-prompt (&key prompt default line-length quit-tag)
  79.   (let ((default-in-a-box (if default
  80.                   (format nil " [DEFAULT=~A]" default)
  81.                 ""))
  82.     (prompt-string (zap-to-string prompt))
  83.     (quit-string (if quit-tag " or QUIT" "")))
  84.     (format-text-if-its-too-long
  85.      (concatenate 'string prompt-string default-in-a-box quit-string)
  86.      line-length)))
  87.  
  88. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  89. ; insist-upon-correct-type
  90. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  91.  
  92. (defun insist-upon-correct-type (type &key echo-stream quittable?)
  93.   (chastise-careless-users)
  94.   (let ((pretty (format nil "Input should be ~A~A~%"
  95.                   (type-pretty-print-string type)
  96.                   (if quittable? " or QUIT" ""))))
  97.     (format t pretty)
  98.     (if echo-stream (format echo-stream pretty))))
  99.  
  100.  
  101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  102. ; chastise-careless-users
  103. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  104.  
  105. (defun chastise-careless-users ()
  106.   (let ((message (chastise-careless-users-string)))
  107.     (if (> (length message) 0)
  108.         (format t "~A~%"
  109.                 (format-text-if-its-too-long message (line-length))))))
  110.  
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112. ; line-length
  113. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  114.  
  115. (defun line-length () *line-length*)
  116.  
  117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  118. ; display-numbered-menu
  119. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  120.  
  121. (defun display-numbered-menu (menu &key special echo-stream)
  122.   (let ((s (numbered-list-string menu :special special :indent 2)))
  123.     (format t s)
  124.     (if echo-stream (format echo-stream s))))
  125.  
  126. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  127. ; prompt-with-default
  128. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  129.  
  130. (defun prompt-with-default
  131.   (&key type echo-stream prompt default quit-tag)
  132.   (let* ((entire-prompt (format-prompt :prompt prompt
  133.                        :default default
  134.                        :line-length (line-length)
  135.                        :quit-tag quit-tag))
  136.          (result
  137.           (block outer
  138.             (format t "~A~%" entire-prompt)
  139.             (if echo-stream (format echo-stream "~A~%" entire-prompt))
  140.             (if default
  141.               (if (equal (peek-char) #\newline)
  142.                 (progn (read-char)
  143.                        (return-from outer default))))
  144.  
  145.             (let ((impurity (read-line)))
  146.               (test-for-quit impurity quit-tag echo-stream)
  147.               (case type
  148.                 (string impurity)
  149.                 (t (read-from-string impurity)))))))
  150.  
  151.               
  152.     (if echo-stream (format echo-stream "~A~%" result))
  153.     (test-for-quit result quit-tag echo-stream)
  154.     result))
  155.  
  156. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  157. ; quit-p
  158. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  159.  
  160. (defun quit-p (r)
  161.   (let ((s (zap-to-string r)))
  162.     (if (> (length s) 3)
  163.         (string-equal (string-upcase (subseq s 0 4))
  164.                       "QUIT"))))
  165.  
  166. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  167. ; test-for-quit
  168. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  169.  
  170. (defun test-for-quit (input quit-tag echo-stream)
  171.   (if (and quit-tag (quit-p input))
  172.       (progn
  173.         (if echo-stream
  174.             (format echo-stream "QUIT!~%"))
  175.         (throw quit-tag t))))
  176.  
  177.  
  178. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  179. ; get-menu-pick-index
  180. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  181. ; returns a zero-origin index to menu
  182.  
  183. (defun get-menu-pick-index
  184.   (menu &key title echo-stream default special quit-tag)
  185.   (let* ((ul-prompt "Enter the number of one menu entry")
  186.          (menu-big (length menu)))
  187.       (if title
  188.         (progn (format t "~A~%" title)
  189.                (if echo-stream (format echo-stream "~A~%" title))))
  190.       (display-numbered-menu menu
  191.         :echo-stream echo-stream
  192.         :special special)
  193.       (1-
  194.         (ask :prompt ul-prompt
  195.              :type `(integer 1 ,menu-big)
  196.              :echo-stream echo-stream
  197.              :default default
  198.              :quit-tag quit-tag))))
  199.  
  200. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  201. ; get-menu-pick
  202. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  203.  
  204. (defun get-menu-pick
  205.   (menu &key title echo-stream default special quit-tag)
  206.   (let ((index (get-menu-pick-index menu
  207.                                     :title title
  208.                                     :echo-stream echo-stream
  209.                                     :default default
  210.                     :special special
  211.                     :quit-tag quit-tag)))
  212.     (elt menu index)))
  213.  
  214. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  215. ; get-one-of-these-characters
  216. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  217.  
  218. (defun get-one-of-these-characters (character-bag)
  219.   (let ((done nil)
  220.         (result nil))
  221.     (while (not done)
  222.       (setq result (get-char))
  223.       (write-char result)
  224.       (terpri)
  225.       (if (member result character-bag)
  226.           (setq done t)
  227.           (progn
  228.             (chastise-careless-users)
  229.             (format t "~A~%"
  230.               (format-text-if-its-too-long
  231.                 (format nil
  232.                   "Please enter one of {~A}"
  233.                   (apply #'concatenate
  234.                     (cons 'string
  235.                       (mapcar #'zap-to-string character-bag))))
  236.                 (line-length))))))
  237.     result))
  238.